﻿using System;

using RpcClient.Interface;
using RpcClient.RpcSysEvent;
using RpcClient.SystemConfig;
using RpcHelper.Config;
using RpcModel;

namespace RpcClient.Collect
{
    [Attr.ClassLifetimeAttr(Attr.ClassLifetimeType.单例)]
    internal class SysConfigCollect : ISysConfig
    {
        private static IConfigServer _ConfigServer = new SystemConfig.SysRemoteConfig();

        private static event Action<IConfigServer, string> _RefreshEvent;

        static SysConfigCollect()
        {
            RemoteSysEvent.AddEvent<SysConfigRefresh>("Rpc_RefreshConfig", _RefreshConfig);
            LocalConfig.Local.RefreshEvent += _Local_RefreshEvent;
        }

        private static readonly string _ConfigMd5 = null;

        public IConfigServer Config => _ConfigServer;

        private static TcpRemoteReply _RefreshConfig(SysConfigRefresh obj)
        {
            if (obj.ConfigMd5 == _ConfigMd5)
            {
                return null;
            }
            _ConfigServer.LoadConfig();
            return null;
        }
        private static void _Local_RefreshEvent(IConfigCollect obj, string name)
        {
            _Refresh(name);
        }

        public void SetLocalServer(IConfigServer server)
        {
            _ConfigServer = new LocalSysConfig(server);
        }

        /// <summary>
        /// 注册配置刷新事件
        /// </summary>
        /// <param name="action"></param>
        public void AddRefreshEvent(Action<IConfigServer, string> action)
        {
            _RefreshEvent += action;
            action(_ConfigServer, string.Empty);
        }
        /// <summary>
        /// 覆盖现有配置刷新事件
        /// </summary>
        /// <param name="action"></param>
        public void SetRefreshEvent(Action<IConfigServer, string> action)
        {
            _RefreshEvent = action;
            action(_ConfigServer, string.Empty);
        }
        internal static void LoadSysConfig()
        {
            _ConfigServer.LoadConfig();
            _Refresh(string.Empty);
        }
        private static void _Refresh(string name)
        {
            _RefreshEvent?.Invoke(_ConfigServer, name);
        }

        public string GetConfigVal(string name)
        {
            return _ConfigServer.GetConfigVal(name);
        }
        public T GetConfigVal<T>(string name)
        {
            return _ConfigServer.GetConfigVal<T>(name);
        }

        public T GetConfigVal<T>(string name, T defValue)
        {
            return _ConfigServer.GetConfigVal<T>(name, defValue);
        }
    }
}
